home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pctv3n2.zip / POSTNET.C < prev    next >
Text File  |  1992-03-19  |  3KB  |  87 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define LONGBAR  33
  7. #define SHORTBAR 32
  8.  
  9. void  postnet(char *),
  10.       printbar(int);
  11.  
  12. void main(int argc, char *argv[]) {
  13. /*-------------------------------------------------------------*/
  14. /*  main function - syntax: Postnet zip_code LETTER|BUSINESS   */
  15. /*-------------------------------------------------------------*/
  16.  
  17. if (argc < 3) {
  18.   printf("Usage:  Envelope zip_code LETTER|BUSINESS\n");
  19.   exit(1);
  20. }
  21.  
  22. if (stricmp(argv[2], "LETTER") == 0)   // size of envelope parameter
  23.   fprintf(stdprn, "\033*p1640x1200Y"); // initialize print position
  24. else
  25.   if (stricmp(argv[2], "BUSINESS") == 0)
  26.     fprintf(stdprn, "\033*p1715x1200Y");
  27.   else {
  28.     printf("Usage:  Envelope zip_code LETTER|BUSINESS\n");
  29.     exit(1);
  30.     }
  31.  
  32. postnet(argv[1]);          // print the US PostNet barcode
  33. fprintf(stdprn, "\033E");  // reset & form feed
  34. }
  35.  
  36. void postnet(char *zip_code) {
  37. /*  postnet - print US Postal Service PostNet barcode.  */
  38. int checksum = 0,
  39.     code[10] = {      // |-len-|--data---|
  40.                 184,  //  1 0 1 1 1 0 0 0\
  41.                 163,  //  1 0 1 0 0 0 1 1 \       len:
  42.                 165,  //  1 0 1 0 0 1 0 1   \     # of bars
  43.                 166,  //  1 0 1 0 0 1 1 0     \   in code
  44.                 169,  //  1 0 1 0 1 0 0 1       \
  45.                 170,  //  1 0 1 0 1 0 1 0        /
  46.                 172,  //  1 0 1 0 1 1 0 0      /  data bits:
  47.                 177,  //  1 0 1 1 0 0 0 1    /    0 = short bar
  48.                 178,  //  1 0 1 1 0 0 1 0  /      1 = long bar
  49.                 180}, //  1 0 1 1 0 1 0 0/
  50.     i,
  51.     len;
  52.  
  53. len = strlen(zip_code);
  54.  
  55. printbar(LONGBAR);               // start with LONGBAR
  56.  
  57. for (i = 0; i < len; i++)        // print each digit
  58.   if (isdigit(zip_code[i])) {
  59.     printbar(code[zip_code[i] - '0']);
  60.  
  61.     checksum = checksum + (zip_code[i] - '0');
  62.   }
  63.  
  64. checksum %= 10;                  // get remainder
  65. printbar(code[10 - checksum]);   // print checksum
  66. printbar(LONGBAR);               // end with LONGBAR
  67.  
  68. }
  69.  
  70. void printbar(int code) {
  71. /*  printbar() - print the bar(s) for a single code. */
  72. int     i,
  73.     len;
  74.  
  75. len = (code >> 5) & 0x07;
  76.  
  77. for (i = len - 1; i >= 0; i--)
  78.   if ((code & (1 << i)) != 0) {     // if bit == 1, long bar
  79.     fprintf(stdprn, "\033*c38a4b0P");    // draw rectangle 38x4 dots
  80.     fprintf(stdprn, "\033*p-14Y");  // move 14 dots (Y) for next bar
  81.     } else {                        // else, short bar
  82.        fprintf(stdprn, "\033*p+21X");    // move 21 dots (X)
  83.        fprintf(stdprn, "\033*c17a4b0P"); // draw rectangle 17x4 dots
  84.        fprintf(stdprn, "\033*p-21x-14Y");// move back 21 dots (X) &
  85.     }                                    // 14 dots (Y) for next bar
  86. }
  87.